﻿using System;
using System.Collections.Generic;
using System.IO;

namespace ClockwiseClient
{
    public class ClassStockTick
    {
        private List<StockTickItem> lstStockTick = new List<StockTickItem>();
        public void LoadStockTick(int market, string code)
        {
            string filename;
            switch (market)
            {
                case 0:
                    filename = ClassStock.strShWorkDir + @"\wShTick\";
                    break;
                default:
                case 1:
                    filename = ClassStock.strSzWorkDir + @"\wSzTick\";
                    break;
                case 2:
                    filename = ClassStock.strHkWorkDir + @"\wHkTick\";
                    break;
                case 3:
                    filename = ClassStock.strUsWorkDir + @"\wUsTick\";
                    break;
            }
            try
            {
                filename += code + ".dat";
                FileStream fsTick = new FileStream(filename, FileMode.Open);
                lstStockTick.Clear();
                byte[] tick = new byte[GetTickItemSize()];
                int i, count = (int)(fsTick.Length / GetTickItemSize());
                for (i = 0; i < count; i++)
                {
                    fsTick.Read(tick, 0, tick.Length);
                    StockTickItem bt = new StockTickItem();
                    bt.Convert(tick);
                    lstStockTick.Add(bt);
                }
                fsTick.Close();

            } catch(Exception)
            {

            }

        }

        public int GetTickCount()
        {
            return lstStockTick.Count;
        }

        public StockTickItem GetTickItem(int idx)
        {
            if(idx >= 0 && idx < lstStockTick.Count) return lstStockTick[idx];
            return null;
        }

        private int GetTickItemSize()
        {
            return 128;
        }

        public class StockTickItem
        {
            public float time = 0;
            public float price = 0;
            public float diff = 0;
            public float change = 0;
            public float preclose = 0;
            public float open = 0;
            public float high = 0;
            public float low = 0;
            public float amplitude = 0;
            public float volume = 0;
            public float amount = 0;
            public float buy = 0;
            public float sell = 0;
            public float delta = 0;
            public float buy2 = 0;
            public float buy3 = 0;
            public float buy4 = 0;
            public float buy5 = 0;
            public float sell2 = 0;
            public float sell3 = 0;
            public float sell4 = 0;
            public float sell5 = 0;
            public float buyV = 0;
            public float buyV2 = 0;
            public float buyV3 = 0;
            public float buyV4 = 0;
            public float buyV5 = 0;
            public float sellV = 0;
            public float sellV2 = 0;
            public float sellV3 = 0;
            public float sellV4 = 0;
            public float sellV5 = 0;

            public void Convert(byte[] buff)
            {
                int pos = 0;
                time = BitConverter.ToSingle(buff, pos); pos += 4;
                price = BitConverter.ToSingle(buff, pos); pos += 4;
                diff = BitConverter.ToSingle(buff, pos); pos += 4;
                change = BitConverter.ToSingle(buff, pos); pos += 4;
                preclose = BitConverter.ToSingle(buff, pos); pos += 4;
                open = BitConverter.ToSingle(buff, pos); pos += 4;
                high = BitConverter.ToSingle(buff, pos); pos += 4;
                low = BitConverter.ToSingle(buff, pos); pos += 4;
                amplitude = BitConverter.ToSingle(buff, pos); pos += 4;
                volume = BitConverter.ToSingle(buff, pos); pos += 4;
                amount = BitConverter.ToSingle(buff, pos); pos += 4;
                buy = BitConverter.ToSingle(buff, pos); pos += 4;
                sell = BitConverter.ToSingle(buff, pos); pos += 4;
                delta = BitConverter.ToSingle(buff, pos); pos += 4;
                buy2 = BitConverter.ToSingle(buff, pos); pos += 4;
                buy3 = BitConverter.ToSingle(buff, pos); pos += 4;
                buy4 = BitConverter.ToSingle(buff, pos); pos += 4;
                buy5 = BitConverter.ToSingle(buff, pos); pos += 4;
                sell2 = BitConverter.ToSingle(buff, pos); pos += 4;
                sell3 = BitConverter.ToSingle(buff, pos); pos += 4;
                sell4 = BitConverter.ToSingle(buff, pos); pos += 4;
                sell5 = BitConverter.ToSingle(buff, pos); pos += 4;
                buyV = BitConverter.ToSingle(buff, pos); pos += 4;
                buyV2 = BitConverter.ToSingle(buff, pos); pos += 4;
                buyV3 = BitConverter.ToSingle(buff, pos); pos += 4;
                buyV4 = BitConverter.ToSingle(buff, pos); pos += 4;
                buyV5 = BitConverter.ToSingle(buff, pos); pos += 4;
                sellV = BitConverter.ToSingle(buff, pos); pos += 4;
                sellV2 = BitConverter.ToSingle(buff, pos); pos += 4;
                sellV3 = BitConverter.ToSingle(buff, pos); pos += 4;
                sellV4 = BitConverter.ToSingle(buff, pos); pos += 4;
                sellV5 = BitConverter.ToSingle(buff, pos);
            }
        }
    }
}
